<?php
class Directory_photo_model extends MY_Model {
    function Directory_photo_model() {
        parent::MY_Model();        
        $this->directory_photo_table = "directory_photo";
    }
    
    function get_photo($company_id){
        //$photo = $this->directory_photo_table;
        if($company_id != ""){
            $this->db->where('id_company',$company_id);
            $this->db->order_by("id", "desc");
            $query = $this->db->get($this->directory_photo_table);
            //$this->db->get_where($this->directory_photo_table, array('id_company' => $company_id));
             
            if($query->num_rows() > 0){
                return $query->result_array();
            }else{
                return FALSE;
            }
        }else{
            return FALSE;
        }
    }
    function add_photo($company_id, $file_name, $caption, $file_ori=''){
      $array_insert = array(
          'id_company'=>$company_id,
          'image'=>$file_name,
          'caption'=>$caption,
          'original'=> $file_ori,
          //'created_date'=>date('Y-m-d H:i:s')
          );
      $this->db->insert($this->directory_photo_table, $array_insert);
      //return $this->db->insert_id();
    }
    
    function update_photo($photo_id, $file_name, $caption, $file_ori=''){
      $array_update = array(
          'image'=>$file_name,
          'caption'=>$caption,
          'original'=> $file_ori,
          );
      $this->db->where('id',$photo_id);
      $this->db->update($this->directory_photo_table, $array_update);
      return $this->db->last_query();
    }
    
    function delete_photo($photo_id){
        $query = $this->db->get_where($this->directory_photo_table, array('id' => $photo_id));
        $photo = $query->row_array();
        if(file_exists("./uploads/directory/photo/preview/".$photo['image'])){
            @unlink("./uploads/directory/photo/preview/".$photo['image']);       
        }
        if(file_exists("./uploads/directory/photo/crop/".$photo['image'])){
            @unlink("./uploads/directory/photo/crop/".$photo['image']);       
        }    
        if(file_exists("./uploads/directory/photo/thumb/".$photo['image'])){
            @unlink("./uploads/directory/photo/thumb/".$photo['image']);       
        }
        if(file_exists("./uploads/directory/photo/".$photo['original'])){
            @unlink("./uploads/directory/photo/".$photo['original']);       
        }
        $this->db->delete($this->directory_photo_table, array('id' => $photo_id)); 
    }
}
?>
